from django.shortcuts import render
from .models import User
from django.http import HttpResponse
from django.contrib.auth.hashers import make_password, check_password

# global data
error_data = {
    'error': ''
}


# 注册
def register_view(request):
    # get 返回页面
    if request.method == 'GET':
        return render(request, "user/register.html")

        # post 处理提交数据
        '''
            1.用户名和密码不能为空
            2.当前用户名是否可用
            3.两个密码要一致
            4.插入数据
        '''
    elif request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        repeat_password = request.POST['repeat_password']

        user = User.objects.filter(username=username)

        if username == '' or username is None:
            error_data['error'] = '用户名不能为空'

        if password == '' or password is None:
            error_data['error'] = '密码不能为空'

        if repeat_password == '' or repeat_password is None:
            error_data['error'] = '请校验密码'

        if user:
            error_data['error'] = '用户名已存在'

        if password != repeat_password and repeat_password is not '':
            error_data['error'] = '两次输入密码不一致'

        if error_data['error'] != '':
            return render(request, "user/register.html", {'error_data': error_data})

        # 密码加密
        hash_password = make_password(password)

        # 加密成功
        if check_password(password, hash_password):
            try:
                user = User.objects.create(username=username, password=hash_password)
            except Exception as e:
                print("--create user error %s" % e)
                error_data['error'] = '用户名已存在'
                return render(request, "user/register.html", {'error_data': error_data})

        # 免密登录
        request.session['username'] = username
        request.session['uid'] = user.id

        return HttpResponse("注册成功")


# 登录
def login_view(request):
    # 请求登录页面
    if request.method == 'GET':
        # 先注册后登录的,肯定有session
        if request.session.get("username") and request.session.get("uid"):
            return render(request, "index/index.html")

        # 上次登录的cookie有效期内再次登录,免登录
        cookie_username = request.COOKIES.get("username")
        cookie_id = request.COOKIES.get("uid")
        if cookie_username and cookie_id:
            request.session['username'] = cookie_username
            request.session['uid'] = cookie_id
            return render(request, "index/index.html")

        return render(request, "user/login.html")

    # 提交表单
    elif request.method == 'POST':
        # 判断用户名是否存在
        user = User.objects.filter(username=request.POST['username'])

        # 不存在
        if len(user) == 0:
            error_data['error'] = '用户不存在'
            return render(request, "user/login.html", {'error_data': error_data})

        # 存在
        # 如果存在,肯定就只有一个,因为用户名是unique
        else:
            # 判断密码正确与否

            # 前端登录传过来的明文密码
            password = request.POST['password']
            user = user[0]

            # 密码正确
            if check_password(password, user.password):
                response = render(request, "index/index.html")

                # 有这个键就是勾选了 记住我
                if 'remember_me' in request.POST:
                    # 设置cookie
                    response.set_cookie("username", user.username, 3600 * 24 * 3)
                    response.set_cookie("uid", user.id, 3600 * 24 * 3)
                # 没勾选就设置session
                else:
                    request.session['username'] = user.username
                    request.session['uid'] = user.id
                return response

            # 密码错误
            else:
                error_data['error'] = '密码错误'
                return render(request, "user/login.html", {'error_data': error_data})
